package com.GlobalPaint.app.ui.Laborer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.EventBusben;
import com.GlobalPaint.app.ui.HTML.PublishHtml;
import com.GlobalPaint.app.utils.MyViewPager;
import com.GlobalPaint.app.utils.UiUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.finalteam.toolsfinal.adapter.FragmentAdapter;

import static com.GlobalPaint.app.R.id.iv_goback;



public class LaborerFragment extends Fragment {
    @BindView(iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_job_wanted)
    TextView tvJobWanted;
    @BindView(R.id.vw_job_wanted)
    View vwJobWanted;
    @BindView(R.id.ll_job_wanted)
    LinearLayout llJobWanted;
    @BindView(R.id.tv_recruit)
    TextView tvRecruit;
    @BindView(R.id.vw_recruit)
    View vwRecruit;
    @BindView(R.id.ll_recruit)
    LinearLayout llRecruit;
    @BindView(R.id.tv_temporary_work)
    TextView tvTemporaryWork;
    @BindView(R.id.vw_temporary_work)
    View vwTemporaryWork;
    @BindView(R.id.ll_temporary_work)
    LinearLayout llTemporaryWork;
    Unbinder unbinder;
    @BindView(R.id.view_pager)
    MyViewPager viewPager;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    private JobWantedFragment jobWantedFragment;
    private RecruitFragment recruitFragment;
    private TemporaryWorkFragment temporaryWorkFragment;
    private PopupWindow popupWindow;
    private LocalReceiver localReceiver;
    private IntentFilter intentFilter;

    private LocalBroadcastManager localBroadcastManager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_laborer, container, false);
        EventBus.getDefault().register(this);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        //初始化TabLayout的title数据集
        List<String> titles = new ArrayList<>();
        titles.add("");
        titles.add("");
        titles.add("");
        jobWantedFragment = new JobWantedFragment();
        recruitFragment = new RecruitFragment();
        temporaryWorkFragment = new TemporaryWorkFragment();
        //初始化ViewPager的数据集
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(jobWantedFragment);
        fragments.add(recruitFragment);
        fragments.add(temporaryWorkFragment);
        //创建ViewPager的adapter
        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), fragments, titles);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);
        localBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        intentFilter=new IntentFilter();
        intentFilter.addAction("refresh");
        localReceiver=new LocalReceiver();

        localBroadcastManager.registerReceiver(localReceiver, intentFilter);
        return view;
    }

    private void initView(View v) {
        ivGoback.setVisibility(View.GONE);
        tvCommonCenter.setText("用工");
        tvFresh.setVisibility(View.VISIBLE);
        tvFresh.setText("发布");
    }
    @OnClick({iv_goback, R.id.ll_job_wanted, R.id.ll_recruit, R.id.ll_temporary_work,R.id.tv_fresh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_job_wanted:
                tvJobWanted.setTextColor(Color.rgb(37, 168, 37));
                tvRecruit.setTextColor(Color.rgb(144, 144, 144));
                tvTemporaryWork.setTextColor(Color.rgb(144, 144, 144));
                vwJobWanted.setVisibility(View.VISIBLE);
                vwRecruit.setVisibility(View.GONE);
                vwTemporaryWork.setVisibility(View.GONE);
                viewPager.setCurrentItem(0);
                break;
            case R.id.ll_recruit:
                tvJobWanted.setTextColor(Color.rgb(144, 144, 144));
                tvRecruit.setTextColor(Color.rgb(37, 168, 37));
                tvTemporaryWork.setTextColor(Color.rgb(144, 144, 144));
                vwJobWanted.setVisibility(View.GONE);
                vwRecruit.setVisibility(View.VISIBLE);
                vwTemporaryWork.setVisibility(View.GONE);
                viewPager.setCurrentItem(1);
                break;
            case R.id.ll_temporary_work:
                tvJobWanted.setTextColor(Color.rgb(144, 144, 144));
                tvRecruit.setTextColor(Color.rgb(144, 144, 144));
                tvTemporaryWork.setTextColor(Color.rgb(37, 168, 37));
                vwJobWanted.setVisibility(View.GONE);
                vwRecruit.setVisibility(View.GONE);
                vwTemporaryWork.setVisibility(View.VISIBLE);
                viewPager.setCurrentItem(2);
                break;
            case R.id.tv_fresh:
                selectFabu();
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void FirstEvent(EventBusben message) {
        String msg = message.getNr();
        if (msg == "qiuzhi" ){
            jobWantedFragment.job();
        } else if (msg == "zhaogong") {
            recruitFragment.job();
        }else if (msg == "linshi") {
            temporaryWorkFragment.job();
        }

    }
    /**
     * 发布寻物和招领
     */
    private void selectFabu() {
        backgroundAlpha(0.7f);//点击出现阴影
        View popupView = LayoutInflater.from(getContext()).inflate(R.layout.lostpop_up, null);
        TextView tvXtwu = (TextView) popupView.findViewById(R.id.lost_xtwu);
        TextView tvVclk = (TextView) popupView.findViewById(R.id.lost_vclk);
        TextView linshi = (TextView) popupView.findViewById(R.id.lost_linshi);
        tvXtwu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(getContext(),PublishHtml.class);
                intent1.putExtra("flid","1");//求职
                startActivity(intent1);
                dis();
            }
        });
        tvVclk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(getContext(),PublishHtml.class);
                intent1.putExtra("flid","0");//招工
                startActivity(intent1);
                dis();
            }
        });
        linshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(getContext(),PublishHtml.class);
                intent1.putExtra("flid","2");//临时招工
                startActivity(intent1);
                dis();
            }
        });

        popupWindow = new PopupWindow(popupView, UiUtil.dp2px(100), UiUtil.dp2px(150));
        Resources resources = getResources();
        Drawable drawable = resources.getDrawable(R.drawable.lost_pop);
        popupWindow.setBackgroundDrawable(drawable);
        // 设置可以获取焦点
        popupWindow.setFocusable(true);
        // 设置可以触摸弹出框以外的区域
        popupWindow.setOutsideTouchable(true);
        //消失的监听
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams dis = getActivity().getWindow().getAttributes();
                dis.alpha = 1f;
                getActivity().getWindow().setAttributes(dis);

            }
        });
        // 更新popupwindow的状态
        popupWindow.update();
        popupWindow.showAsDropDown(tvFresh, UiUtil.dp2px(-60), UiUtil.dp2px(15));

    }

    //使弹窗的阴影消失
    private void dis() {
        WindowManager.LayoutParams dis = getActivity().getWindow().getAttributes();
        dis.alpha = 1f;
        getActivity().getWindow().setAttributes(dis);
        popupWindow.dismiss();
    }

    public void backgroundAlpha(float bgAlpha) {
        //   设置背景颜色变暗
        WindowManager.LayoutParams lpidentity = getActivity().getWindow().getAttributes();
        lpidentity.alpha = bgAlpha;
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getActivity().getWindow().setAttributes(lpidentity);
    }
    //接受广播 回调mess页面
    class LocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            //Toast.makeText(context,"dddddd",Toast.LENGTH_LONG).show();
            temporaryWorkFragment.job();
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        localBroadcastManager.unregisterReceiver(localReceiver);
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }
}
